from DrissionPage import Chromium
from utils.file_operation import save_dict_to_json
# 在9222端口启动浏览器同时创建对象，如果浏览器已经存在，则接管它
browser = Chromium(9222)
tab1 = browser.latest_tab
print(tab1.title, id(tab1))

data_list = []
# 监听数据包
listen_url = "store/athena/search/main"
tab1.listen.start(listen_url)

# tab1.refresh()
for num in range(1, 31):
    print(f"点击{num}页")
    # 选择分页器
    pagination_dom = tab1.ele(
        "xpath://*[@id='reactRoot']/div[1]/div/div[2]/div/div/div[2]"
    )
    # 选择下一页
    next_page_dom = pagination_dom.ele(".=zen_page").child(-1)
    # 点击下一页
    next_page_dom.click()
    # 等待数据包
    data_packet = tab1.listen.wait()
    # 获取相应jso数据
    resp_data = data_packet.response.raw_body
    data_list.append(resp_data)
    # 等待2-3秒
    browser.wait(2, 3)

save_dict_to_json(data_list, "data_list.json")

# for _ in range(5):
#     tab1("@class=zen_fs-14 zen_bdr-4 ZenPagination-page ZenPagination-next ").click()  # 点击下一页
#     res = tab1.listen.wait()  # 等待并获取一个数据包
#     browser.wait(2, 3)
#     print(res.url)
#     print(res.__dict__)
